<?php
// Load the QCubed Development Framework
require('../qcubed.inc.php');



class MeetingListForm extends QForm {
    protected $dtgMeetings;
    // Override Form Event Handlers as Needed
//		protected function Form_Run() {}

//		protected function Form_Load() {}

//		protected function Form_Create() {}
    //Perm
    protected $authSys;
    protected function Form_Run() {
        $this->authSys = new Perm();
        $this->authSys->IsAuthenticated(true);
    }


    protected function Form_Create() {
        // Instantiate the Meta DataGrid
        $this->dtgMeetings = new MeetingDataGrid($this);

        $this->dtgMeetings->CssClass = 'datagrid';
        $this->dtgMeetings->AlternateRowStyle->CssClass = 'alternate';

        $this->dtgMeetings->Paginator = new QPaginator($this->dtgMeetings);
        $this->dtgMeetings->ItemsPerPage = 20;


        $this->dtgMeetings->AddRowAction(new QMouseOverEvent(), new QCssClassAction('selectedStyle'));
        $this->dtgMeetings->AddRowAction(new QMouseOutEvent(), new QCssClassAction());

        $this->dtgMeetings->RowActionParameterHtml = '<?= $_ITEM->Id ?>';
        $this->dtgMeetings->AddRowAction(new QClickEvent(), new QAjaxAction('dtgMeetingsRow_Click'));

        $this->dtgMeetings->SetDataBinder("dtg_Bind");


        $this->dtgMeetings->MetaAddColumn('Subject');
        $this->dtgMeetings->MetaAddColumn('Location');
        $this->dtgMeetings->MetaAddColumn('Date');


        $Col="";
        $Col = new QDataGridColumn('Attendance', '<?= $_FORM->HtmlAttendance_Render($_ITEM) ?>', 'Width=400', array (
                        'OrderByClause' => QQ::OrderBy(QQN::Meeting()->PersonAsMembers->Person),
                        'ReverseOrderByClause' => QQ::OrderBy(QQN::Meeting()->PersonAsMembers->Person, false)),  'HtmlEntities=false' );
        //$Col->Filter = QQ::Like(QQN::Meeting()->PersonAsMembers->Person,null);
        //$Col->FilterPrefix = $Col->FilterPostfix = '%';
        
        $Col->FilterType = QFilterType::None;
        //$Col->FilterBoxSize = 70;
        $this->dtgMeetings->AddColumn($Col);
    }

    protected function dtg_Bind() {
        $optClose = array( $this->dtgMeetings->OrderByClause, $this->dtgMeetings->LimitClause);
        /*@PERM*/
        if ( $_SESSION[_SV_PERM_][_SV_PERM_Admin_Role_]== true ) {
            $this->dtgMeetings->TotalItemCount = Meeting::QueryCount($this->dtgMeetings->Conditions);
            $this->dtgMeetings->DataSource = Meeting::QueryArray($this->dtgMeetings->Conditions,
                    QQ::Clause(
                    $this->dtgMeetings->OrderByClause,
                    $this->dtgMeetings->LimitClause
            ));
        }
        else {
            echo "EXIT THIS PART IN BIND WAS NOT YET CREATED";
            exit;
            //CHANGE BELOW
            $this->dtgMeetings->TotalItemCount = Person::CountByOrgArray($this->dtgMeetings->Conditions);
            $this->dtgMeetings->DataSource = Person::LoadArrayByOrgArray($this->dtgMeetings->Conditions,
                    QQ::Clause(
                    $this->dtgMeetings->OrderByClause,
                    $this->dtgMeetings->LimitClause
            ));
        }
    }
    public function strTruncate($string, $length) {
        $output="";
        settype($string, 'string');
        settype($length, 'integer');
        for($a = 0; $a < $length AND $a < strlen($string); $a++) {
            $output .= $string[$a];
        }
        return($output);
    }

    protected function dtgMeetingsRow_Click($strFormId, $strControlId, $strParameter) {

        $intId = intval($strParameter);

        $strEditPageUrl = __MEETING__ . '/meeting_edit.php/'. $intId;
        ;
        QApplication::Redirect($strEditPageUrl);
    }
    public function HtmlAttendance_Render(Meeting $objMeeting) {
        $Persons = $objMeeting->GetPersonAsMembersArray();

        $HTML="";
        foreach ($Persons as $Person){
            if ( ! $HTML == "")
                $HTML .=   " , " . $Person->__toString() ;
            else
                $HTML .= $Person->__toString();
        }
        
        return $HTML;
    }


}


MeetingListForm::Run('MeetingListForm','tpl/meeting_list.tpl.php');
?>